const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        path: app.globalData.path,
        indicatorDots: false,
        vertical: false,
        autoplay: true,
        interval: 2000,
        duration: 500,
        isNoSwiper: false,

        bannerList: [],
        specials: [],

        categories: [],
        notice: '',

        productList: [],

        user: '',
        
        usedPoints: 0, //累计消耗积分
        userScore: 0, //用户积分数
        diamondNum: 0, //钻石数
        userBalance: 0, //账户余额

        notice: '',
    },

    toPage(e) {
        var flag = e.currentTarget.dataset.flag,
            id = e.currentTarget.dataset.id;
        if (flag == 'cart') {
            wx.navigateTo({
                url: '/packageProduct/pages/shoppingCart/shoppingCart',
            })
        } else if (flag == 'notice') {
            wx.navigateTo({
                url: '/packageUser/pages/notice/notice',
            })
        } else if (flag == 'cate') {
            console.log(id);
            wx.setStorageSync('cateId', id)
            wx.switchTab({
                url: '/pages/classifyList/classifyList',
            })
        } else if (flag == 'more') {
            // 专区
            console.log(id);
            wx.navigateTo({
                url: '/packageProduct/pages/zhuanQu/zhuanQu?id=' + id,
            })
        } else if (flag == 'detail') {
            // 商品详情
            wx.navigateTo({
                url: '/packageProduct/pages/productDetail/productDetail?id=' + id,
            })
        } else if (flag == 'search') {
            wx.navigateTo({
                url: '/packageProduct/pages/searchProduct/searchProducts',
            })
        } else if (flag == 'banner') {
            var banner = e.currentTarget.dataset.banner;
            console.log(banner);

            if (banner.bannerHref == 'detail') {
                // 商品详情
                wx.navigateTo({
                    url: '/packageProduct/pages/productDetail/productDetail?id=' + banner.bannerPro,
                })
            } else if (banner.bannerHref == 'zqProduct') {
                // 商品专区
                wx.navigateTo({
                    url: '/packageProduct/pages/zhuanQu/zhuanQu?id=' + banner.bannerPro,
                })
            }
        }
    },

    getMain() {
        var url = app.globalData.url + "main/listMain", that = this;
        wx.showLoading({
            title: '',
        })
        app.wxRequest(url, {
            shopId: app.globalData.shopId,
        }, res => {
            if (res.flag) {
                console.log(res.data);

                res.data.specials.forEach(item => {
                    item.productList = item.productList.filter(ele => {
                        return ele.productStatus == 1
                    })
                })

                that.setData({
                    bannerList: res.data.bannerList,
                    specials: res.data.specials,
                    user: app.globalData.user,
                })
                console.log(this.data);

                wx.stopPullDownRefresh()
                wx.hideLoading()
                that.getUserData()
                that.getList()
            }
        })


    },

    getList() {
        wx.showLoading({
            title: '',
        })
        var url = app.globalData.url + "affiche/listUrl",
            that = this;
        app.wxRequest(url, {
        }, res => {
            console.log(res);
            if (res.flag) {
                var datas = res.data;
                console.log(datas);
                datas = datas.filter(item => {
                    return new Date(item.publishTime).getTime() < new Date().getTime()
                })
                datas = datas.sort((a, b) => {
                    return new Date(b.publishTime).getTime() - new Date(a.publishTime).getTime()
                })
                that.setData({
                    notice: datas[0]
                })
                console.log(datas);
                wx.hideLoading()
                wx.stopPullDownRefresh()
            }
        },
            err => {
                console.log(err);
            })
    },






    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var isLogin = wx.getStorageSync("isZhongHuiLogin"),
            that = this;
        if (!isLogin) {
            app.getInfo().then(function (res) {
                console.log(res);

                app.globalData.user = res.data.user
                app.globalData.openId = res.data.openId
                app.globalData.userId = res.data.user.userId
                app.globalData.shopId = res.data.user.shopId || 0

                wx.setStorageSync('isZhongHuiLogin', true)
                wx.setStorageSync('user', JSON.stringify(res.data.user))
                that.getMain()

                if (res.data.user.userName == null) {
                    wx.showModal({
                        title: '提示',
                        content: '请完善您的信息',
                        cancelText: '先逛逛',
                        confirmText: '立即完善',
                        complete: (res) => {
                            if (res.cancel) {
                            }
                            if (res.confirm) {
                                wx.navigateTo({
                                    url: '/packageUser/pages/userInfo/userInfo',
                                })
                            }
                        }
                    })
                }

            })
        } else {
            var user = app.globalData.user;
            if (user.userName == null) {
                wx.showModal({
                    title: '提示',
                    content: '请完善您的信息',
                    cancelText: '再等等',
                    confirmText: '立即完善',
                    complete: (res) => {
                        if (res.cancel) {
                        }
                        if (res.confirm) {
                            wx.navigateTo({
                                url: '/packageUser/pages/userInfo/userInfo',
                            })
                        }
                    }
                })
            }

            that.getMain()
        }

    },
    getUserData() {
        wx.showLoading({
            title: '加载中',
        })
        var url = app.globalData.url + "points/listLowerUser",
            data = {
                userId: app.globalData.userId,
            };
        app.wxRequest(url, data, res => {
            console.log(res.data);
            if (res.flag) {
                wx.hideLoading()
                if (res.data != null) {
                    app.globalData.vipFlag = res.data.user.note
                }
            }
        })
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {



    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        this.getMain()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    onShareTimeline() { }
})